/*
 * postmodeISCPpacketForMulticast.java
 *
 * Created on January 22, 2007, 8:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.sf.iSnake.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;

/**
 *
 * @author Jitendra
 *
 * Packet used by server for multicasting the players informations to all the players
 */
public class postmodeISCPpacketForMulticast implements Serializable, Cloneable  {
    
    private Short postmodeMulticastCommand;                   // Command packets that will only arrive at the RECEIVE thread
    
    /**
     * NOTE that the index in vector for each player will be 1 less that their player id
     * So if a player has player_id = 2, then you'll find that player's info in index (2-1) = 1 position in the respective vector
     * THIS IS BECAUSE VECTOR INDEXES MUST START WITH ZERO
     */
    // objects of type postmodeISCPpacketForSend will be added to this vector
    private Short[][] playersDxDy;                                  // Change in x and y coordinate of all players
    
    private Short foodConsumedBy;                                   // player_id which has consumed the food
    private GameAreaCoordinate currentFood;
    
    private Vector playersLeft;                                     // Will contain the player_ids of all the player(s) who have left the game
    
    private boolean snakeCollided;
    private Short collidedPlayerCount;
    private Short[] collidedPlayers;

    // Source: Core Java - Volume 2 , Seventh Edition    Page No. 704
    public Object clone()
    {
        try {
            // save the object to a byte array
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bout);
            out.writeObject(this);
            out.close();
            
            // read a clone of the object from the byte array
            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
            ObjectInputStream in = new ObjectInputStream(bin);
            Object ret = in.readObject();
            in.close();
            
            return ret;
        }
        catch(Exception e)  {
            e.printStackTrace();
            return null;
        }
    }
    
    public postmodeISCPpacketForMulticast()    {
        postmodeMulticastCommand = new Short("0");
        setSnakeCollided(false);
        this.collidedPlayerCount = 0;
        this.collidedPlayers = new Short[10];
        this.foodConsumedBy=new Short("0");
    }
    
    public void clearAllMembers()   {
        postmodeMulticastCommand = new Short("0");
        this.setCurrentFood(null);
        this.foodConsumedBy = null;
        this.playersDxDy = null;
        this.playersLeft = null;
        this.collidedPlayerCount = 0;
        this.collidedPlayers = new Short[10];
    }
    
    public Short getFoodConsumedBy() {
        return foodConsumedBy;
    }

    public void setFoodConsumedBy(Short foodConsumedBy) {
        this.foodConsumedBy = foodConsumedBy;
    }

    public Vector getPlayersLeft() {
        return playersLeft;
    }

    public void setPlayersLeft(Vector playersLeft) {
        this.playersLeft = playersLeft;
    }

    public Short getPostmodeMulticastCommand() {
        return postmodeMulticastCommand;
    }

    public void setPostmodeMulticastCommand(Short postmodeMulticastCommand) {
        this.postmodeMulticastCommand = postmodeMulticastCommand;
    }

    public Short[][] getPlayersDxDy() {
        return playersDxDy;
    }

    public void setPlayersDxDy(Short[][] playersDxDy) {
        this.playersDxDy = playersDxDy;
    }

    public GameAreaCoordinate getCurrentFood() {
        return currentFood;
    }

    public void setCurrentFood(GameAreaCoordinate currentFood) {
        this.currentFood = currentFood;
    }

    public boolean isSnakeCollided() {
        return snakeCollided;
    }

    public void setSnakeCollided(boolean snakeCollided) {
        this.snakeCollided = snakeCollided;
    }

    public Short getCollidedPlayerCount() {
        return collidedPlayerCount;
    }

    public void setCollidedPlayers(Short collidedPlayers) {
        this.collidedPlayers[getCollidedPlayerCount()] = collidedPlayers;
        this.collidedPlayerCount++;
    }

    public boolean isPlayerCollided(Short player_id)   {
        for(int i=0;i<this.collidedPlayerCount;i++)   {
            if(this.collidedPlayers[i].equals(player_id))
                return true;
        }
        return false;
    }
}